package org.example.struct.array;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @author liyishan
 * @date 2023/12/26 09:47
 * @apiNote
 */

public class Test00720LongestWord {

    public static String longestWord(String[] words) {
        Arrays.sort(words,(a,b)->{
            if(a.length() != b.length()){
                return a.length()-b.length();
            }else{
                return b.compareTo(a);
            }
        });
        String longest = "";
        Set<String> candidates = new HashSet<>();
        candidates.add("");
        int  n = words.length;
        for(int i = 0; i < n; i++){
            String word = words[i];
            if(candidates.contains(word.substring(0,word.length()-1))){
                candidates.add(word);
                longest = word;
            }
        }
        return longest;
    }
}
